/*     */ package com.wzh.service.base;
/*     */ 
/*     */ import com.wzh.bean.QueryResult;
/*     */ import com.wzh.utils.GenericsUtils;
/*     */ import java.beans.BeanInfo;
/*     */ import java.beans.Introspector;
/*     */ import java.beans.PropertyDescriptor;
/*     */ import java.io.Serializable;
/*     */ import java.lang.reflect.Method;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.Map;
import java.util.Map.Entry;
/*     */ import javax.persistence.EmbeddedId;
/*     */ import javax.persistence.Entity;
/*     */ import javax.persistence.EntityManager;
/*     */ import javax.persistence.PersistenceContext;
/*     */ import javax.persistence.Query;
/*     */ import org.springframework.transaction.annotation.Propagation;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Transactional
/*     */ public abstract class DaoSupport<T>
/*     */   implements DAO<T>
/*     */ {
/*     */ 
/*     */   @PersistenceContext
/*     */   protected EntityManager em;
/*  29 */   protected Class<T> entityClass = GenericsUtils.getSuperClassGenericType(getClass());
/*     */ 
/*     */   public void delete(Serializable[] entityids)
/*     */   {
/*  33 */     for (Serializable entityid : entityids)
/*  34 */       this.em.remove(this.em.getReference(this.entityClass, entityid));
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public T find(Serializable entityid)
/*     */   {
/*  41 */     if (entityid == null)
/*  42 */       throw new RuntimeException(this.entityClass.getName() + ":传入的实体id不能为空");
/*  43 */     return this.em.find(this.entityClass, entityid);
/*     */   }
/*     */ 
/*     */   public void save(T entity)
/*     */   {
/*  48 */     this.em.persist(entity);
/*     */   }
/*     */ 
/*     */   public void update(T entity)
/*     */   {
/*  54 */     this.em.merge(entity);
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public QueryResult<T> getScrollData(int firstindex, int maxresult)
/*     */   {
/*  63 */     return getScrollData(firstindex, maxresult, null, null, null);
/*     */   }
/*     */ 
/*     */   public QueryResult<T> getScrollData()
/*     */   {
/*  69 */     return getScrollData(-1, -1, null, null, null);
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public QueryResult<T> getScrollData(int firstindex, int maxresult, LinkedHashMap<String, String> orderby)
/*     */   {
/*  76 */     return getScrollData(firstindex, maxresult, null, null, orderby);
/*     */   }
/*     */ 
/*     */   public QueryResult<T> getScrollData(int firstindex, int maxresult, String wherejpql, Object[] params)
/*     */   {
/*  84 */     return getScrollData(firstindex, maxresult, wherejpql, params, null);
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public QueryResult<T> getScrollData(int firstindex, int maxresult, String wherejpql, Object[] params, LinkedHashMap<String, String> orderby)
/*     */   {
/*  92 */     QueryResult qr = new QueryResult();
/*  93 */     Query query = this.em.createQuery("select o from " + getEntityName() + " o " + ((wherejpql == null) || ("".equals(wherejpql)) ? "" : new StringBuilder("where ").append(wherejpql).toString()) + getOrderByJpql(orderby));
/*  94 */     setQueryParam(query, params);
/*     */ 
/*  96 */     if ((firstindex != -1) && (maxresult != -1)) {
/*  97 */       query.setFirstResult(firstindex).setMaxResults(maxresult);
/*     */     }
/*     */ 
/* 100 */     qr.setResultlist(query.getResultList());
/*     */ 
/* 102 */     query = this.em.createQuery("select count(" + getCountField(this.entityClass) + ") from " + getEntityName() + " o " + ((wherejpql == null) || ("".equals(wherejpql)) ? "" : new StringBuilder("where ").append(wherejpql).toString()));
/* 103 */     setQueryParam(query, params);
/* 104 */     qr.setTotalrecord(((Long)query.getSingleResult()).longValue());
/*     */ 
/* 106 */     return qr;
/*     */   }
/*     */ 
/*     */   protected void setQueryParam(Query query, Object[] params)
/*     */   {
/* 114 */     if ((params != null) && (params.length > 0))
/* 115 */       for (int i = 0; i < params.length; i++)
/* 116 */         query.setParameter(i + 1, params[i]);
/*     */   }
/*     */ 
/*     */   public String getOrderByJpql(LinkedHashMap<String, String> orderby)
/*     */   {
/* 127 */     StringBuilder orderjpql = new StringBuilder("");
/* 128 */     if ((orderby != null) && (orderby.size() > 0)) {
/* 129 */       orderjpql.append(" order by ");
/* 130 */       for (Map.Entry entity : orderby.entrySet()) {
/* 131 */         orderjpql.append("o.")
/* 132 */           .append((String)entity.getKey()).append(" ").append((String)entity.getValue()).append(",");
/*     */       }
/* 134 */       orderjpql.deleteCharAt(orderjpql.length() - 1);
/*     */     }
/* 136 */     return orderjpql.toString();
/*     */   }
/*     */ 
/*     */   protected String getEntityName()
/*     */   {
/* 145 */     String entityName = this.entityClass.getSimpleName();
/* 146 */     Entity entity = (Entity)this.entityClass.getAnnotation(Entity.class);
/* 147 */     if ((entity.name() != null) && (!"".equals(entity.name()))) {
/* 148 */       entityName = entity.name();
/*     */     }
/*     */ 
/* 151 */     return entityName;
/*     */   }
/*     */ 
/*     */   public long getCount()
/*     */   {
/* 156 */     Query query = this.em.createQuery("select count(" + getCountField(this.entityClass) + ") from " + getEntityName() + " o ");
/* 157 */     return ((Long)query.getSingleResult()).longValue();
/*     */   }
/*     */ 
/*     */   protected static <E> String getCountField(Class<E> clazz)
/*     */   {
/* 167 */     String out = "o";
/*     */     try {
/* 169 */       PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
/* 170 */       for (PropertyDescriptor propertydesc : propertyDescriptors) {
/* 171 */         Method method = propertydesc.getReadMethod();
/* 172 */         if ((method != null) && (method.isAnnotationPresent(EmbeddedId.class))) {
/* 173 */           PropertyDescriptor[] ps = Introspector.getBeanInfo(propertydesc.getPropertyType()).getPropertyDescriptors();
/* 174 */           out = "o." + propertydesc.getName() + "." + (!ps[1].getName().equals("class") ? ps[1].getName() : ps[0].getName());
/* 175 */           break;
/*     */         }
/*     */       }
/*     */     } catch (Exception e) {
/* 179 */       e.printStackTrace();
/*     */     }
/* 181 */     return out;
/*     */   }
/*     */ }

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.service.base.DaoSupport
 * JD-Core Version:    0.6.1
 */